package crossover;

import java.util.ArrayList;
import java.util.List;

import neural.Individual;
import neural.Net;

import util.RandomUtils;

/*
 * Si claramente multipoint puede ser una subclase de este, pero bue... ya lo hice asi copy+paste <3
 */
public class AnularCrossover extends AbstractCrossover {
	int l;
	public AnularCrossover(int l) {
		int networkLength = Net.getInstance().listSize();
		if( l <= 1 || l > networkLength/2){
			throw new IllegalArgumentException("l tiene que estar entre 1 y L/2");
		}
		this.l = l;
	}




	@Override
	protected List<Individual> generateChilds(Individual father2, Individual mother2) {
		List<Double> father = father2.getWeights();
		List<Double> mother = mother2.getWeights();
		
		List<Double> son1 = new ArrayList<Double>();
		List<Double> son2 = new ArrayList<Double>();
		
		// selecionarlocus al azar
		int locus = RandomUtils.randomBetween(0, father.size());
		if( locus + l <= father.size()){
			son1.addAll(father.subList(0, locus));
			son2.addAll(mother.subList(0, locus));
			son1.addAll(mother.subList(locus, locus + l));
			son2.addAll(father.subList(locus, locus + l));
			son1.addAll(father.subList(locus + l, father.size()));
			son2.addAll(mother.subList(locus + l, father.size()));
		}else{
			int aux = l - (father.size() - locus);
			son1.addAll(father.subList(0, aux));
			son2.addAll(mother.subList(0, aux));
			son1.addAll(mother.subList(aux, locus));
			son2.addAll(father.subList(aux, locus));
			son1.addAll(father.subList(locus, father.size()));
			son2.addAll(mother.subList(locus, father.size()));
		}
		
//		System.out.println("hijo1" + son1);
//		System.out.println("hijo2" + son2);
		List<Individual> resp = new ArrayList<Individual>();
		resp.add(new Individual(son1));
		resp.add(new Individual(son2));
		return resp;
	}
	
}
